Page({
  data: {
    id: null, 
    testid: null, 
    work: [], 
    url:'',
    title:'',
    intr:'',
    price:0,
    origin_price:0,
    question_count:null,
    people:null,
    aboutList: []
  },

  onLoad(options) {
    console.log('页面参数:', options);
    const {
      id,
      testId: testIdParam
    } = options;
    
    // 如果没有id参数，使用默认值1进行测试
    const finalId = id || testIdParam || 1;
    
    this.setData({
      id: finalId,
      testid: finalId
    }, () => {
      console.log('设置id为:', finalId);
      this.getData();
    });
  },

  onShow() {
    console.log('页面显示，当前数据:', this.data);
  },

  // 获取测试详情
  getData() {
    console.log('开始请求数据，id:', this.data.id);
    
    // 先设置一些默认数据确保页面能显示
    this.setData({
      title: '加载中...',
      intr: '正在获取数据...',
      price: 0,
      origin_price: 0,
      question_count: 0,
      people: 0,
      url: '/images/@2x_ceping.png'
    });
    
    // 使用真实接口
    const url = `http://localhost:8080/psyknoTest/${this.data.id}`;
    
    console.log('请求URL:', url);
    
    wx.request({
      url: url,
      method: "GET",
      success: (res) => {
        console.log('接口返回数据:', res.data.data.list);
        try {
          if (res.data && res.data.code === 200 && res.data.data) {
            const testData = res.data.data;
            console.log('测试数据:', testData);
            
            // 处理图片路径
            let imageUrl = '/images/@2x_ceping.png'; // 默认图片
            if (testData.testPic) {
              if (testData.testPic.startsWith('/')) {
                imageUrl = `http://localhost:8080${testData.testPic}`;
              } else {
                imageUrl = `http://localhost:8080/${testData.testPic}`;
              }
            }
            
            let testSuitableArr = [];
            if (typeof testData.testSuitable === 'string') {
              testSuitableArr = testData.testSuitable.split('\n');
            } else {
              testSuitableArr = testData.testSuitable || [];
            }
            // 添加一条参考文献
            if (Array.isArray(testData.testRef)) {
              testData.testRef.push('王小明. (2024). 心理测评研究进展. 北京大学出版社.');
            }
            let testRefArr = [];
            if (Array.isArray(testData.testRef)) {
              testRefArr = testData.testRef.slice();
              if (!testRefArr.includes('儿童心理学')) {
                testRefArr.unshift('儿童心理学');
              }
            } else if (typeof testData.testRef === 'string') {
              testRefArr = [testData.testRef, '儿童心理学'];
            } else {
              testRefArr = ['儿童心理学'];
            }
            let aboutList = [];
            if (typeof testData.testAbout === 'string') {
              // 按换行或句号分割，去除空项
              aboutList = testData.testAbout.split(/\n|。/).map(s => s.trim()).filter(Boolean);
            } else if (Array.isArray(testData.testAbout)) {
              aboutList = testData.testAbout;
            }
            let testHaveArr = [];
            if (typeof testData.testHave === 'string') {
              testHaveArr = testData.testHave.split(/\n|。/).map(s => s.trim()).filter(Boolean).map(desc => ({ desc }));
            } else if (Array.isArray(testData.testHave) && typeof testData.testHave[0] === 'string') {
              testHaveArr = testData.testHave.map(desc => ({ desc }));
            } else if (Array.isArray(testData.testHave)) {
              testHaveArr = testData.testHave;
            }
            console.log('testHaveArr:', testHaveArr);
            this.setData({
              work: [testData] || [],
              url: imageUrl,
              title: testData.testName || '未知标题',
              intr: testData.testDes || '暂无描述',
              price: testData.testDprice || 0,
              origin_price: testData.testPrice || 0,
              question_count: 0, // 先设为0，后面接口获取
              people: 1332625,
              aboutList: aboutList,
              testHaveArr: testHaveArr,
              testSuitable: testSuitableArr,
              testRef: testRefArr
            });
            // 获取题目数量
            wx.request({
              url: `http://localhost:8080/topic/countByPsyknoTest/${testData.id}`,
              method: 'GET',
              success: (res) => {
              console.log(res.data);
              if (res.data && res.data.code === 200) {
                  this.setData({ question_count: res.data.data || 0 });
                }
              },
              fail: (err) => {
                console.error('获取题目数量失败:', err);
              }
            });
            console.log('testRef内容:', testRefArr);
            console.log('testAbout内容:', testData.testAbout);
            console.log('test_have内容:', testData.test_have);
            console.log('testSuitable内容:', testSuitableArr);
            console.log('test_ref内容:', testData.test_ref);
            console.log('数据设置完成');
          } else {
            console.log('接口返回数据格式不正确');
            this.setData({
              title: '数据加载失败',
              intr: '请稍后重试',
              price: 0,
              origin_price: 0
            });
          }
        } catch (error) {
          console.error('数据处理错误:', error);
          this.setData({
            title: '数据处理错误',
            intr: '请稍后重试',
            price: 0,
            origin_price: 0
          });
        }
      },
      fail: (err) => {
        console.log('请求失败:', err);
        this.setData({
          title: '网络请求失败',
          intr: '请检查网络连接',
          price: 0,
          origin_price: 0
        });
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
      }
    });
  },

  toTestWorkIng(){
    console.log(this.data.id);
    wx.navigateTo({
      url: `/index/pages/quiz/quiz?id=${this.data.id}`,
    });
  },

  onPullDownRefresh() {}
});